package com.whmskj.xjlhsc.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.whmskj.xjlhsc.entity.YsWbPlan;
import com.whmskj.xjlhsc.entity.YsWbRecord;
import com.whmskj.xjlhsc.mapper.YsWbPlanMapper;
import com.whmskj.xjlhsc.mapper.YsWbRecordMapper;
import com.whmskj.xjlhsc.service.YsWbPlanService;
import com.whmskj.xjlhsc.service.YsWbRecordService;
import com.whmskj.xjlhsc.view.entity.vo.MaintenanceTaskVo;
import com.whmskj.xjlhsc.view.entity.vo.ProcessMaintenanceNumVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author 张
 * @version 1.0
 */

@Service
public class YsWbRecordServiceImpl extends ServiceImpl<YsWbRecordMapper, YsWbRecord> implements YsWbRecordService {
    @Autowired
    private YsWbPlanMapper ysWbPlanMapper;

    /**
     * 今日维保任务
     * @return
     */
    @Override
    public List<MaintenanceTaskVo> MaintenanceTaskToday() {
        return ysWbPlanMapper.selectMaintenanceTaskToday(null,null);
    }

    @Override
    public List<ProcessMaintenanceNumVo> processMaintenanceNum(String dateType) {
        List<ProcessMaintenanceNumVo> result=new ArrayList<>();
//        List<MaintenanceTaskVo> list=null;
        if ("1".equals(dateType)){
            //本日
            LocalDate today = LocalDate.now();
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            String startTime = today.format(formatter);
            String endTime = today.format(formatter);
            result = ysWbPlanMapper.selectProcessMaintenanceNum(startTime, endTime);

        } else if ("2".equals(dateType)) {
            //本周

            // 获取今天的日期
            LocalDate today = LocalDate.now(); // 计算本周日期范围（周一到周日）
            LocalDate thisWeekMonday = today.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
            LocalDate thisWeekSunday = today.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY));
            // 计算本周一的日期（若今天是周一，则返回今天）
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            String startTime = thisWeekMonday.format(formatter);
            String endTime = thisWeekSunday.format(formatter);
            result = ysWbPlanMapper.selectProcessMaintenanceNum(startTime, endTime);
        } else if ("3".equals(dateType)) {
            //本月

            // 获取今天的日期
            LocalDate now = LocalDate.now();
            String startTime="";
            String endTime="";
            int dayOfMonth = now.getDayOfMonth();

            if (dayOfMonth>=20){
                startTime= now.getYear()+"-"+now.getMonthValue()+"-"+20;
            }
            else {
                startTime=now.getYear()+"-"+(now.getMonthValue()-1)+"-"+20;
            }
            endTime=now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
            result = ysWbPlanMapper.selectProcessMaintenanceNum(startTime, endTime);
        }
        return result;
    }

    public static void main(String[] args) {
        // 获取今天的日期
        LocalDate today = LocalDate.now();
        // 获取本月1号的日期
        LocalDate firstDayOfMonth = today.minusMonths(7);
    }
}
